package com.linzm.leetcode.primary.exercises1_20230114;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author zimingl
 * @Date 2023/1/15 23:00
 * @Description: 找出数组排序后的目标下标
 */
public class Demo26_2089 {
    /**
     * 输入：nums = [1,2,5,2,3], target = 2
     * 输出：[1,2]
     * 解释：排序后，nums 变为 [1,2,2,3,5] 。
     * 满足 nums[i] == 2 的下标是 1 和 2 。
     */
    public static void main(String[] args) {
        int[] nums = {1,2,5,2,3};
        Demo26_2089 demo26_2089 = new Demo26_2089();
        List<Integer> list = demo26_2089.targetIndices(nums, 2);
        String[] strings = list.stream().map(String::valueOf).toArray(String[]::new);
        System.out.println(String.join(" ", strings));
    }

    private List<Integer> targetIndices(int[] nums, int target) {
        Arrays.sort(nums);
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == target) {
                list.add(i);
            }
        }
        return list;
    }
}
